package com.huitone.gddw.service.impl.analysis.traffic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.analysis.traffic.IDownFlowRepDao;
import com.huitone.gddw.dao.entity.SendSm;
import com.huitone.gddw.dao.entity.SmFlow;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.analysis.traffic.IDownFlowRepService;
import com.huitone.gddw.service.dto.analysis.traffic.DownFlowRepDTO;
import com.huitone.gddw.service.dto.analysis.traffic.DownSmRecDTO;
import com.huitone.gddw.service.system.IRoleService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 *下发短信流量分析 接口实现类
 * @author chengsiyyue
 *
 */
@Service
public class DownFlowRepService implements IDownFlowRepService {

	@Autowired
	private IDownFlowRepDao downFlowRepDao;
	
	@Autowired
	private IRoleService roleService;

	@Override
	public QueryPage<DownFlowRepDTO> selectPage(QueryPage<SmFlow> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isUnit",roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId())?"1":"0");
		map.put("isSvrNode", page.getParams().get("isSvrNode"));
		map.put("isServiceType", page.getParams().get("isServiceType"));
		map.put("isGateLink", page.getParams().get("isGateLink"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("svrNode",page.getParams().get("svrNode"));
		map.put("gateLinkId", page.getParams().get("gateLinkId"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("userId", SysLoginUtil.getLoginUserId());

		List<DownFlowRepDTO> result = downFlowRepDao.selectPage(map);
		
		QueryPage<DownFlowRepDTO> queryPage = new QueryPage<DownFlowRepDTO>();
		queryPage.setResult(result);
		return queryPage;
	}

	@Override
	public List<Map<String, Object>> selectRecords(QueryPage<SmFlow> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resultCode", page.getParams().get("resultCode"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if(format.format(new Date()).equals(page.getParams().get("endDate"))){
			map.put("includeToday", "yes");
		}else {
			map.put("includeToday", "no");
		}
		return downFlowRepDao.queryCountByGate(map);
	}

	@Override
	public List<DownFlowRepDTO> getRecord(Map<String, Object> map) {
		map.put("isUnit",roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId())?"1":"0");
		map.put("userId", SysLoginUtil.getLoginUserId());
		return downFlowRepDao.getRecord(map);
	}

	@Override
	public QueryPage<DownSmRecDTO> selectDetial(QueryPage<SendSm> page) {
		Map<String, Object> map = page.getParams();
		map.put("isUnit",roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId())?"1":"0");
		map.put("userId", SysLoginUtil.getLoginUserId());
		map.put("start", (page.getPageNo() - 1) * page.getPageSize()+1);
		map.put("end", (page.getPageNo() - 1) * page.getPageSize() + page.getPageSize());

		int totalCount = 0;
		
		List<DownSmRecDTO> result = downFlowRepDao.selectDetial(map);
		
		totalCount = Integer.valueOf(result.size()==0 ? "0":result.get(0).getMaxCount());
		
		
//		totalCount = downFlowRepDao.selectCount(map);
		QueryPage<DownSmRecDTO> queryPage = new QueryPage<DownSmRecDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public List<DownSmRecDTO> getRecordDetial(Map<String, Object> map) {
		map.put("isUnit",roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId())?"1":"0");
		map.put("userId", SysLoginUtil.getLoginUserId());
		return downFlowRepDao.getRecordDetial(map);
	}
}
